﻿using Dapper;
using Publicuse.DBCore.MySql;
using Publicuse.Interface;
using Publicuse.Entity;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Service
{
    public class MySqlDapperService : IMySqlDapperService
    {
        private int dbIns = -1;

        public MySqlDapperService()
        {
            this.dbIns = AppConfigUtil.ReadAppSettings("dbMySqlType").GetIntValue();
        }

        /// <summary>
        /// 业务新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public int AddInfo<T>(T t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Add, out msg);
        }

        /// <summary>
        /// 批量业务新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<int> AddInfo<T>(List<T> t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Add, out msg);
        }

        /// <summary>
        /// 业务删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public int DeleteInfo<T>(T t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Delete, out msg);
        }

        public int DeleteInfo<T>(T t, out string msg, string sql) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Delete, out msg, sql);
        }

        /// <summary>
        /// 业务修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public int UpdateInfo<T>(T t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Update, out msg);
        }

        /// <summary>
        /// 批量业务修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<int> UpdateInfo<T>(List<T> t, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).CUDEntity<T>(t, CUDType.Update, out msg);
        }

        /// <summary>
        /// 业务查询列表信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sqlParameters"></param>
        /// <param name="isproc"></param>
        /// <param name="msg"></param>
        /// <param name="list"></param>
        public void GetInfoList<T>(string sql, object[] sqlParameters, bool isproc, out string msg, out List<T> list) where T : class, new()
        {
            throw new Exception("已放弃该方法，请联系管理员");
        }

        /// <summary>
        /// 分页查询列表信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbIns"></param>
        /// <param name="storedprocedure"></param>
        /// <param name="tablename"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sqlWhere"></param>
        /// <param name="sortFile"></param>
        /// <param name="srotType"></param>
        /// <param name="count"></param>
        /// <param name="exception"></param>
        /// <param name="list"></param>
        public void GetInfoList<T>(string storedprocedure, string tablename, string queryFile, int pageIndex, int pageSize, string sqlWhere, string sortFile, string srotType, out long count, out string exception, out List<T> list) where T : class, new()
        {
            DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).GetList<T>(storedprocedure, tablename, queryFile, pageIndex, pageSize, sqlWhere, sortFile, srotType, out count, out exception, out list);
        }

        /// <summary>
        /// 分页查询列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="msg"></param>
        /// <param name="list"></param>
        /// <param name="totalCount"></param>
        public void GetList<T>(CommonQuery query, out string msg, out List<T> list, out long totalCount) where T : class, new()
        {
            DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).GetList<T>(query.storedProcedureName, query.tableName, query.files, query.pageIndex, query.pageSize, query.sqlWhere, query.sortFile, query.sortType, out totalCount, out msg, out list);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="msg"></param>
        /// <param name="list"></param>
        public void GetList<T>(CommonQuery query, out string msg, out List<T> list) where T : class, new()
        {
            throw new Exception("已放弃该方法，请联系管理员");
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="tuples"></param>
        /// <param name="exception"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool GetList<T>(string sql, List<Tuple<string, string, ExcuteType>> tuples, out string exception, out List<T> list) where T : new()
        {
            list = new List<T>();
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).GetList<T>(sql, tuples, out exception, out list);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        public void QueryInfoByID<T>(long id, out string msg, out T data) where T : class, new()
        {
            DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).GetEntityById<T>(id, out msg, out data);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="tuples"></param>
        /// <param name="msg"></param>
        /// <param name="t"></param>
        public void QueryInfoBySql<T>(string sql, List<Tuple<string, string, ExcuteType>> tuples, out string msg, out T t) where T : class, new()
        {
            DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).QueryInfoBySql<T>(sql, tuples, out msg, out t);
        }

        /// <summary>
        /// 获取数据集
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="isproc"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public bool GetDataSet(string sql, out string exception, out DataSet dataSet)
        {
            dataSet = new DataSet();
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).GetDataSet(sql, out exception, out dataSet);
        }

        /// <summary>
        /// 获取数据表
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="tuples"></param>
        /// <param name="exception"></param>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public DataTable GetDataTable(string sqlstr, List<Tuple<string, string, ExcuteType>> tuples, out string exception)
        {
            var dataTable = new DataTable();
            DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).GetDataTable(sqlstr, tuples, out exception, out dataTable);
            return dataTable;
        }

        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="tuples"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool ExecuteSql(string sql, List<Tuple<string, string, ExcuteType>> tuples, out string msg)
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).ExecuteSql(sql, tuples, out msg);
        }

        /// <summary>
        /// 执行批量SQL
        /// </summary>
        /// <param name="pairs"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool ExecuteSqls(List<Tuple<string, Dictionary<string, string>>> pairs, out string msg)
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).ExecuteSql(pairs, out msg);
        }

        /// <summary>
        /// 就是通过json字符串找出更新的字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Update<T>(string json, int id, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Update<T>(json, id, out msg);
        }

        public void QueryInfoByID<T>(long id, object[] sqlParameters, out string msg, out T t) where T : class, new()
        {
            throw new Exception("已放弃该方法,请联系管理员");
        }

        public bool ExecuteSql(string sql, Dictionary<string, string> pairs, out string msg, ExcuteType type = ExcuteType.Equal)
        {
            throw new NotImplementedException();
        }

        public DataSet GetDataSet(string sqlstr, object param, bool isproc, out string exception)
        {
            throw new NotImplementedException();
        }

        public long Insert<T>(T entity, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Insert<T>(entity, out msg);
        }

        public bool Update<T>(T entity, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Update<T>(entity, out msg);
        }

        public bool Delete<T>(long id, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Delete<T>(id, out msg);
        }

        public T QueryFirst<T>(string sql, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).QueryFirst<T>(sql, out msg);
        }

        public IEnumerable<T> Insert<T>(List<T> entitys, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Insert<T>(entitys, out msg);
        }

        public T Update<T>(List<T> entitys, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Update<T>(entitys, out msg);
        }

        public bool Delete<T>(List<string> ids, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Delete<T>(ids, out msg);
        }

        public long Insert(string sql, DynamicParameters parameters, out string msg)
        {
            throw new NotImplementedException();
        }

        public List<T> Query<T>(string sql, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Query<T>(sql, out msg);
        }

        public List<T> GetAll<T>(out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).GetAll<T>(out msg);
        }

        public List<T> Query<T>(string sql, object param, out string msg) where T : class, new()
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Query<T>(sql, param, out msg);
        }

        public bool ExecuteSql(string sql, object param, out string msg)
        {
            msg = string.Empty;
            return (bool)DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns)?.ExecuteSql(sql, param, out msg);
        }

        public DataTable GetDataTable(string sql, object param, out string msg)
        {
            msg = string.Empty;
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns)?.GetDataTable(sql, param, out msg);
        }

        public DataSet GetDataSet(string sql, object param, out string msg)
        {
            return DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).GetDataSet(sql, param, out msg);
        }

        public void QueryMultiple(string sql, object param, out string msg, Action<SqlMapper.GridReader> action)
        {
            DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).QueryMultiple(sql, param, out msg, action);
        }

        public void Query(string sql, object param, out string msg, Action<IDbConnection> action)
        {
            DBHelperUtil<MySqlDapperInstance>.GetInstance(dbIns).Query(sql, param, out msg, action);
        }
    }
}
